/**
  ******************************************************************************
  * @file    /WB_Gneiss_Sample_Code_APP.c
  * @author  Winbond FAE Steam Lin
  * @version V1.1.0
  * @date    09-December-2015
  * @brief   This code provide the Demo code for RPMC operation. Please do not copy the rootkey generate method directly.
		     Rootkey generate method should be keep in secret and should not exposed.		 
			 
  *            
  * COPYRIGHT 2015 Winbond Electronics Corporation.
*/ 
#include "WB_Gneiss_Sample_Code_LLD.h"

int main(void)
{
  unsigned char ROOTKey[32];	// Rootkey array
  unsigned char HMACKey[32];	// HMACkey array
  unsigned char HMACMessage[4]; // HMAC message data, use for update HMAC key
  unsigned char Input_tag[12];	// Input tag data for request conte
  unsigned char Check_tag[12];	// Output tag data for verification
  unsigned char Check_signature[32]; // Output signature
  unsigned char RPMCStatus;
  unsigned int RPMC_counter;
  
  /* first stage, initial rootkey, please not use Winbond's rootkey generate rule method directly */
  /* rootkey information can up to 32byte (256bit)	*/ 
  ROOTKey[0] = 'W';
  ROOTKey[1] = 'I';
  ROOTKey[2] = 'N';
  ROOTKey[3] = 'B';
  ROOTKey[4] = 'O';
  ROOTKey[5] = 'N';
  ROOTKey[6] = 'D';
  
  RPMCStatus = WB_RPMC_WrRootKey(1, ROOTKey);        // initial Rootkey, use first rootkey/counter pair
  
  if(RPMCStatus == 0x80){
    // Write rootkey success
  }
  else{
    // write rootkey fail, check datasheet for the error bit
  }
  /* initial rootkey operation done     */
  
  /* Second stage, update HMACKey after ever power on. without update HMACkey, Gneiss would not function*/
  HMACMessage[0] = rand()%0x100;        // Get random data for HMAC message, it can also be serial number, RTC information and so on.
  HMACMessage[1] = rand()%0x100;
  HMACMessage[2] = rand()%0x100;
  HMACMessage[3] = rand()%0x100;
  
  /* Update HMAC key and get new HMACKey. 
  HMACKey is generated by SW using Rootkey and HMACMessage.
  RPMC would also generate the same HMACKey by HW   */
  
  RPMCStatus = WB_RPMC_UpHMACkey(1, ROOTKey, HMACMessage, HMACKey); 
  if(RPMCStatus == 0x80){
    // update HMACkey success
  }
  else{
    // write HMACkey fail, check datasheet for the error bit
  }
  /* update HMACKey operation done     */
    
  /* Third stage, increase RPMC counter */  
  /* input tag is send in to RPMC, it could be time stamp, serial number and so on*/
  Input_tag[0] = '2';
  Input_tag[1] = '0';
  Input_tag[2] = '1';
  Input_tag[3] = '5';
  Input_tag[4] = '1';
  Input_tag[5] = '1';
  Input_tag[6] = '1';
  Input_tag[7] = '1';
  Input_tag[8] = '2';
  Input_tag[9] = '4';
  RPMCStatus = WB_RPMC_IncCounter(1, HMACKey, Input_tag);
  if(RPMCStatus == 0x80){
    // increase counter success
  }
  else{
    // increase counter fail, check datasheet for the error bit
  }
  /* counter data in stoage in public array counter[], data is available if WB_RPMC_IncCounter() operation successed */
  RPMC_counter = WB_RPMC_ReadCounterData();
  /* increase RPMC counter done*/

  /* Main security operation call challenge*/
  while(1){
	if(WB_RPMC_Challenge(1, HMACKey, Input_tag)!=0){
		/* return signature miss-match */
		return;
	}
  }
}

/************************ (C) COPYRIGHT Winbond Electronics Corporation *****END OF FILE****/
